<template>
  <div class="form-div">
    <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="用户名" prop="account">
        <el-input v-model="form.account" :disabled="edit">
          <template v-if="!edit" #prepend>{{ tenementKey }}_</template>
        </el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!edit">
        <el-input v-model="form.password"/>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="form.deptId" placeholder="请选择部门">
          <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select multiple v-model="selectedRoles" placeholder="请选择角色" :value="echoRoles">
          <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="2">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import {getRoleListApi} from "@/assets/api/system/role";
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import {getLabelListByTypeApi} from "@/assets/api/system/label";
import {editUserApi} from "@/assets/api/system/user";

const props = defineProps({
  form: {
    name: String,
    account: String,
    password: String,
    deptId: Number,
    status: Number,
    remark: String
  },
  edit: Boolean,
  echoRoles: Array,
  deptList: Array
});

const emit = defineEmits(['save', 'close']);

// 获取角色列表
const roleList = ref([]);
const selectedRoles = ref(props.echoRoles);
const getRoleList = () => {
  getRoleListApi().then(res => {
    roleList.value = res.data.data;
  })
}

// 获取当前用户标识
const tenementKey = ref('');
const getTenementKey = () => {
  getLabelListByTypeApi("tenement_key").then(res => {
    tenementKey.value = res.data.data[0].tag;
  })
}

// 表单验证
const formRef = ref();
const roleValidator = (rule, value, callback) => {
  if (selectedRoles.value.length === 0) {
    callback(new Error('请选择角色'));
  } else {
    callback();
  }
}
const rules = ref({
  name: [
    {required: true, message: '请输入姓名', trigger: 'blur'},
  ],
  account: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ],
  deptId: [
    {required: true, message: '请选择部门', trigger: 'change'},
  ],
  roles: [
    {required: true, validator: roleValidator, trigger: 'change'},
  ],
  status: [
    {required: true, message: '请选择状态', trigger: 'change'},
  ],
});

// 保存
const save = async (formEl) => {
  if (tenementKey.value === '') {
    ElMessage.error('获取当前用户标识失败');
    return;
  }
  props.form.roleIds = selectedRoles.value.join(",");

  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (!props.form.account.includes(tenementKey.value))
        props.form.account = tenementKey.value + "_" + props.form.account;
      editUserApi(props.form).then(res => {
        if (res.data.code === 200) {
          ElMessage.success('保存成功');
          emit('save', true);
        } else {
          ElMessage.error(res.data.msg);
        }
      })
    }
  })
}

onMounted(() => {
  getTenementKey();
  getRoleList();
})
</script>

<style scoped>

</style>